package com.example.tablistdemo.slice;

import com.example.tablistdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutManager;
import ohos.agp.components.TabList;
import ohos.agp.text.Layout;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 获取TabList组件实例
        TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        // 设置文字大小
        tabList.setTabTextSize(80);
        // 设置布局方向为水平布局
        tabList.setOrientation(Component.HORIZONTAL);
        // 设置选中指示器的高度
        tabList.setSelectedTabIndicatorHeight(10);
        // 设置标签是否自动拉伸充满页签栏
        tabList.setFixedMode(true);
        // 创建标签
        TabList.Tab tab = tabList.new Tab(getContext());
        // 设置标签文案
        tab.setText("分类1");
        // 向TabList中添加标签
        tabList.addTab(tab);

        TabList.Tab tab2 = tabList.new Tab(getContext());
        tab2.setText("分类2");
        tabList.addTab(tab2);

        TabList.Tab tab3 = tabList.new Tab(getContext());
        tab3.setText("分类3");
        tabList.addTab(tab3);

        // 设置默认选中的标签
        tab.select();

        // 设置宽度为内容宽度
        tabList.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        // 设置高度为充满父容器
        tabList.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
        // 设置布局方向为竖直
        tabList.setOrientation(Component.VERTICAL);
        // 设置指示器的样式风格
        tabList.setIndicatorType(TabList.INDICATOR_OVAL);

        // 设置标签尺寸自定
//        tabList.setFixedMode(false);
        // 设置标签在布局方向上的尺寸
//        tabList.setTabLength(800);
        // 设置标签间的间隔
//        tabList.setTabMargin(400);

        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                // 标签选中时回调
                HiLog.info(new HiLogLabel(0,0,"tablist"),"选中新的标签:%d", tab.getPosition());
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
                // 标签取消选中时回调
                HiLog.info(new HiLogLabel(0,0,"tablist"),"取消选中的标签:%d", tab.getPosition());
            }

            @Override
            public void onReselected(TabList.Tab tab) {
                // 已经选中的标签再次点击
                HiLog.info(new HiLogLabel(0,0,"tablist"),"重复选中新的标签:%d", tab.getPosition());
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
